/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import junit.framework.TestCase;





/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.
   UrlValidator url = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
   
   public UrlValidatorTest(String testName) {
      super(testName);
   }

   
   
   public void testManualTest()
   {
	   assertTrue(url.isValid("http://www.wikipedia.org"));
	   assertTrue(url.isValid("http://www.wikipedia.org/"));
	   assertTrue(url.isValid("http://www.wikipedia.org/more"));
	   //assertTrue(url.isValid("www.wikipedia.org")); // <-- This url fails when it shouldn't.
	   assertTrue(url.isValid("http://www.wikipedia.org/more/more/"));
	   assertTrue(url.isValid("http://www.amazon.ca"));
	   assertTrue(url.isValid("ftp://www.amazon.ca"));
	   assertTrue(url.isValid("https://www.amazon.ca/books"));
	   assertFalse(url.isValid("http://www.amazon"));
	   assertFalse(url.isValid("amazon"));
	   assertFalse(url.isValid("http://www.amazon.ca/../"));   
   }
   
   
   public void testYourFirstPartition()
   {
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertFalse(url.isValid("http://www.amazon.zz"));
		//assertTrue(url.isValid("http://www.amazon.co.uk")); // <-- This one fails when it shouldn't.
		//assertTrue(url.isValid("http://www.amazon.org.uk")); // <-- Also discovered this one fails when it shouldn't.
		assertFalse(url.isValid("http://www.go/.com")); 
		assertTrue(url.isValid("http://go.com"));
		assertFalse(url.isValid("http://testone.tests?.com"));
		assertFalse(url.isValid("http://bad domain.com"));
   }
   
   public void testYourSecondPartition(){
		//assertTrue(url.isValid("http://www.amazon.co.uk")); // <-- This one also fails when it shouldn't.
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertTrue(url.isValid("http://www.wikipedia.edu"));
		assertTrue(url.isValid("http://www.wikipedia.net"));
		assertTrue(url.isValid("http://www.wikipedia.biz"));
		assertTrue(url.isValid("http://www.wikipedia.edu")); 
		assertTrue(url.isValid("http://www.wikipedia.mobi"));
		assertFalse(url.isValid("http://www.wikipedia.co3"));
		assertFalse(url.isValid("http://www.wikipedia.thisistoolong"));
   }
   //unit test 1
   public void testIsValid()
   {
	   
	   UrlValidator url = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES); 
	   int count = 1;
	   boolean compare = true;
	  
	   for(int i = 0;i<20;i++)
	   {
		   //random index for corresponding url parts
		   int iScheme = (int)(Math.random()*UrlScheme.length);
		   int iAuth = (int)(Math.random()*UrlAuthority.length);
		   int iPort = (int)(Math.random()*UrlPort.length);
		   int iPath = (int)(Math.random()*Path.length);
		   int iQuery = (int)(Math.random()*UrlQuery.length);
		   int iPathOptions = (int)(Math.random()*UrlPathOptions.length);
		   
		   //concatenating all url parts to make one link
		   String testUrlPath = UrlScheme[iScheme][0]+UrlAuthority[iAuth][0]+UrlPort[iPort][0]+Path[iPath][0]+UrlQuery[iQuery][0];
		   String testUrlPathOptions = UrlScheme[iScheme][0]+UrlAuthority[iAuth][0]+UrlPort[iPort][0]+UrlPathOptions[iPathOptions][0]+UrlQuery[iQuery][0];
		   
		   //testing links with path and PathPptions
		   boolean resultPath =  url.isValid(testUrlPath);
		   boolean resultPathOptions = url.isValid(testUrlPathOptions);
		   
		   //testing pass/fail for Paths
		   if ((Boolean.valueOf(UrlScheme[iScheme][1]) && Boolean.valueOf(UrlAuthority[iAuth][1]) && Boolean.valueOf(UrlPort[iPort][1]) && Boolean.valueOf(Path[iPath][1]) && Boolean.valueOf(UrlQuery[iQuery][1]))==resultPath){
			   System.out.println(count + "." + " pass: "  + testUrlPath);
		   }else{//if in this case then test fails we want assert to fail to trigger JUnit notification
			   System.out.println(count + "." + " fail: " + testUrlPath);
			   compare = false;
		   }
		   count++;
		   
		   //testing pass/fail for PathOptions
		   if ((Boolean.valueOf(UrlScheme[iScheme][1]) && Boolean.valueOf(UrlAuthority[iAuth][1]) && Boolean.valueOf(UrlPort[iPort][1]) && Boolean.valueOf(UrlPathOptions[iPath][1]) && Boolean.valueOf(UrlQuery[iQuery][1]))==resultPathOptions){
			   System.out.println(count + "." + " pass: " + testUrlPathOptions);
		   }else{//if in this case then test fails we want assert to fail to trigger JUnit notification
			   System.out.println(count + "." + " fail: " + testUrlPathOptions);
			   compare = false;
		   }
		   count++;
	   }
	   //lets JUnit trigger pass/fail of testIsValid()
	   assertTrue(compare);	
   }
   
   public void testAnyOtherUnitTest()
   {
	   
   }

   String[][] UrlScheme = {{"http://", "true"},
           {"ftp://", "true"},
           {"h3t://", "true"},
           {"3ht://", "false"},
           {"http:/", "false"},
           {"http:", "false"},
           {"http/", "false"},
           {"://", "false"},
           {"", "true"}};

	String[][] UrlAuthority = {{"www.google.com", "true"},
            {"go.com", "true"},
            {"go.au", "true"},
            {"0.0.0.0", "true"},
            {"255.255.255.255", "true"},
            {"256.256.256.256", "false"},
            {"255.com", "true"},
            {"1.2.3.4.5", "false"},
            {"1.2.3.4.", "false"},
            {"1.2.3", "false"},
            {".1.2.3.4", "false"},
            {"go.a", "false"},
            {"go.a1a", "false"},
            {"go.cc", "true"},
            {"go.1aa", "false"},
            {"aaa.", "false"},
            {".aaa", "false"},
            {"aaa", "false"},
            {"", "false"}
	};
	String[][] UrlPort = {{":80", "true"},
	         {":65535", "true"},
	         {":0", "true"},
	         {"", "true"},
	         {":-1", "false"},
	         {":65636", "true"},
	         {":65a", "false"}
	};
	String[][] Path = {{"/test1", "true"},
			{"/t123", "true"},
			{"/$23", "true"},
			{"/..", "false"},
			{"/../", "false"},
			{"/test1/", "true"},
			{"", "true"},
			{"/test1/file", "true"},
			{"/..//file", "false"},
			{"/test1//file", "false"}
	};
	//Test allow2slash, noFragment
	String[][] UrlPathOptions = {{"/test1", "true"},
               {"/t123", "true"},
               {"/$23", "true"},
               {"/..", "false"},
               {"/../", "false"},
               {"/test1/", "true"},
               {"/#", "false"},
               {"", "true"},
               {"/test1/file", "true"},
               {"/t123/file", "true"},
               {"/$23/file", "true"},
               {"/../file", "false"},
               {"/..//file", "false"},
               {"/test1//file", "true"},
               {"/#/file", "false"}
	};

	String[][] UrlQuery = {{"?action=view", "true"},
          {"?action=edit&mode=up", "true"},
          {"", "true"}
	};

   }

